/*
 ============================================================================
 Name		: ResponseHomepage.cpp
 Author	  : wangchao
 Version	 : 1.0
 Copyright   :
 Description : CResponseHomepage implementation
 ============================================================================
 */

#include "ResponseHomepage.h"
#include "ResponseDefinses.h"
#include "ProjectsDefines.h"
#include "ResponseHomepageData.h"
#include "ResponseMagazineItemData.h"
#include "BetterUserHBufC.h"

CResponseHomepage::CResponseHomepage( )
{
    // No implementation required
}

CResponseHomepage::~CResponseHomepage( )
{
    SAFE_DELETE( _data );
}

CResponseHomepage* CResponseHomepage::NewLC( )
{
    CResponseHomepage* self = new ( ELeave ) CResponseHomepage( );
    CleanupStack::PushL( self );
    self->ConstructL( );
    return self;
}

CResponseHomepage* CResponseHomepage::NewL( )
{
    CResponseHomepage* self = CResponseHomepage::NewLC( );
    CleanupStack::Pop( ); // self;
    return self;
}

void CResponseHomepage::ConstructL( )
{
    _data = CResponseHomepageData::NewL( );
}

void CResponseHomepage::LoadMagazineItemsNode( CElement* elementRecommend, RPointerArray< CResponseMagazineItemData >* container )
{

    int count = elementRecommend->ElementCount( );
    for( int index = 0; index < count; index++ )
    {
        CElement* elementItem = elementRecommend->GetElement( index );
        CResponseMagazineItemData* magazineItem = ResponseMagazineItemDataParser::Parse( elementItem );
        if( magazineItem )
        {
            container->Append( magazineItem );
        }
    }
}

void CResponseHomepage::LoadMagazineCatesNode( CElement* elementCate, RPointerArray< CResponseMagazineCateData >* cates )
{
    int count = elementCate->ElementCount( );
    for( int index = 0; index < count; index++ )
    {
        CElement* elementItem = elementCate->GetElement( index );
        if( !elementItem )
        {
            continue;
        }
        CResponseMagazineCateData* cate = CResponseMagazineCateData::NewL( );

        cate->_c_id = BetterUtility::GetElementTextAsInt( elementItem->GetElement( kReponseIDNode ) );
        cate->_name = BetterUtility::GetElementText( elementItem->GetElement( kReponseNameNode ) );
        cate->_url = BetterUtility::GetElementText( elementItem->GetElement( kResponseUrlNode ) );

        CElement* element = elementItem->GetElement( kReponseActionNode );
        if( element )
        {
            const TDesC& action = element->GetElmentText( );
            _LIT( kActionTypeHot, "hot" );
            _LIT( kActionTypeSort, "sort" );
            _LIT( kActionTypeGroupBuy, "group_buy" );

            if( action == kActionTypeHot )
            {
                cate->_type = CResponseMagazineCateData::EHot;
            }
            else if( action == kActionTypeSort )
            {
                cate->_type = CResponseMagazineCateData::ESort;
            }
            else if( action == kActionTypeGroupBuy )
            {
                cate->_type = CResponseMagazineCateData::EGroupBuy;
            }
            else
            {
                cate->_type = CResponseMagazineCateData::ECate;
            }
        }

        cates->Append( cate );
    }
}

void CResponseHomepage::LoadSearchTags( CElement* elementSearch, CDesC16Array* array )
{
    int count = elementSearch->ElementCount( );
    for( int index = 0; index < count; index++ )
    {
        CElement* element = elementSearch->GetElement( index );
        if( element && element->GetName( ) == kResponseItemNode )
        {
            CElement* elementTitle = element->GetElement( kReponseTitleNode );
            if( elementTitle )
            {
                array->AppendL( elementTitle->GetElmentText( ) );
            }
        }
    }
}

void CResponseHomepage::LoadValidResponseFromXmlNode( CElement* elementResponse )
{
    _ASSERT_LOG( elementResponse != NULL );

    if( !elementResponse )
    {
        return;
    }

    CElement* elementResult = elementResponse->GetElement( kResponseResultNode );
    _ASSERT_LOG( elementResult );

    if( !elementResult )
    {
        return;
    }

    int count = elementResult->ElementCount( );
    for( int index = 0; index < count; index++ )
    {
        CElement* element = elementResult->GetElement( index );
        if( !element )
        {
            continue;
        }
        if( element->GetName( ) == kResponseRecommendNode )
        {
            LoadMagazineItemsNode( element, &_data->_recommend );
        }
        else if( element->GetName( ) == kResponseChoicestNode )
        {
            LoadMagazineItemsNode( element, &_data->_choicest );
        }
        else if( element->GetName( ) == kResponseNewestNode )
        {
            LoadMagazineItemsNode( element, &_data->_magazines );
        }
        else if( element->GetName( ) == kResponseTopCateNode )
        {
            LoadMagazineCatesNode( element, &_data->_cates1 );
        }
        else if( element->GetName( ) == kResponseShortUrlNode )
        {
            LoadMagazineCatesNode( element, &_data->_cates2 );
        }
        else if( element->GetName( ) == kResponseHotSearchNode )
        {
            _data->_tags->Reset( );
            LoadSearchTags( element, _data->_tags );// _data->_tags->AppendL( );
        }
        else
        {

        }
    }
}
